var app = getApp();
var request = app.request;
var setting = app.globalData.setting;
var util = require('../../../utils/util.js');

Page({
    data: {
        url: setting.url,
        resourceUrl: setting.resourceUrl,
        defaultAvatar: setting.resourceUrl + "/static/images/user68.jpg",
        head_pic: null,
        distribut: {},
        webUrl: '',
    },

    onShow: function () {
        this.requestDistribut();
    },

    onPullDownRefresh: function (e) {
        this.requestDistribut();
    },

    requestDistribut: function () {
        var that = this;
        request.get('/api/distribut/index', {
            success: function (res) {
                var distribut = res.data.result;
                var head_pic = distribut.user.head_pic;
                if (head_pic.indexOf('http') == -1){
                    head_pic = that.data.url + head_pic;
                }
                that.setData({ head_pic: head_pic });
                distribut.storeTimeFomate = util.format(distribut.store_time, 'yyyy-MM-dd');
                that.setData({ distribut: distribut });
                wx.stopPullDownRefresh();
            }
        });
    },

    /** 分销订单 */
    distributOrder: function () {
        if (this.data.distribut.store_time){
            wx.navigateTo({ url: '/pages/distribut/order_list/order_list' });
        } else {
            app.showWarning('暂未开店');
        }
    },

    /** 我的团队 */
    distributTeam: function () {
        if (this.data.distribut.store_time) {
            wx.navigateTo({ url: '/pages/distribut/team/team' });
        } else {
            app.showWarning('暂未开店');
        }
    },

    /** 我的名片 */
    distributCard: function () {  
            if (this.data.distribut.store_time) {
                wx.navigateTo({ url: '/pages/distribut/poster/poster' });                   
            } else {
                app.showWarning('暂未开店');
            }               
    },

    /** 分销排行 */
    distributSort: function () {
        if (this.data.distribut.store_time) {
            this.setData({ webUrl: "/api/distribut/rankings/order/distribut_money" });
            wx.navigateTo({ url: '/pages/index/webview/webview' });
        } else {
            app.showWarning('暂未开店');
        }
    },

    /** 新手必看 */
    distributSee: function () {
        this.setData({ webUrl: "/api/distribut/must_see" });
        wx.navigateTo({ url: '/pages/index/webview/webview' });
    },

    /** 查看网店 */
    distributShop: function () {
        if (this.data.distribut.store_time) {
            wx.navigateTo({ url: '/pages/distribut/my_store/my_store' });
        } else {
            app.showWarning('暂未开店');
        }
    },

    /** 佣金明细 */
    distributMoney: function () {
        if (this.data.distribut.store_time) {
            this.setData({ webUrl: "/api/distribut/rebate_log" });
            wx.navigateTo({ url: '/pages/index/webview/webview' });
        } else {
            app.showWarning('暂未开店');
        }
    },

    /** 分销商品列表 */
    goodsList: function () {
        if (this.data.distribut.store_time) {
            wx.navigateTo({ url: '/pages/distribut/good_list/good_list' });
        } else {
            app.showWarning('暂未开店');
        }
    },

})